package com.deer.wms.ware.task.web;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.model.CustInfoBO;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.project.root.util.HttpClient;
import com.deer.wms.system.manage.model.DataDictDetail;
import com.deer.wms.system.manage.service.DataDictDetailService;
import com.deer.wms.ware.task.model.WCS.WcsInfo;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;

import java.util.ArrayList;
import java.util.List;

/**
* Created by  on 2020/06/14.
*/
@RestController
@RequestMapping("/wcs/infos")
public class WcsInfoController {

    @Autowired
    private PushErpService pushErpService;

    private static  WcsInfo wcsInfo ;
    @Autowired
    private DataDictDetailService dataDictDetailService;


    @GetMapping("/getWcsInfo1")
    @ResponseBody
    public static WcsInfo getWcsInfo1(){
        if(wcsInfo==null){
            return  null;
        }else {
            /*Date now = DateUtils.getNowDate();
            long diff = now.getTime() - wcsInfo.getCreateTime().getTime();
            if(diff>5000){  //19895
                return null;
            }*/
            return  wcsInfo;
        }

    }
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody WcsInfo wcsInfo1) {
        wcsInfo = wcsInfo1;
        wcsInfo.setCreateTime(DateUtils.getNowDateTimeString());
        // wcsInfoService.save(wcsInfo);
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/getWcsInfo")
    @ResponseBody
    public Result getWcsInfo() {
        return ResultGenerator.genSuccessResult(wcsInfo);
    }

    @GetMapping("convert/{number}")
    public void convert(@PathVariable Integer number) {
        List<DataDictDetail> dataDictDetails = new ArrayList<>();
        String token = pushErpService.getToken();
        if (ObjectUtil.isNotEmpty(token)) {
            String url;
            url = "https://syecrg.sieyuan.com:44398/api/Item/GetAllItems/" + number;
            //调用erp接口获取数据
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("Accept", "application/json");
            httpHeaders.add("Authorization", "Bearer " + token);
            String result = null;

            try {
                result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<CustInfoBO> di = JSONObject.parseArray(jsonArray.toJSONString(), CustInfoBO.class);
                System.out.println(di);
                for (CustInfoBO custInfoBO : di) {
                    DataDictDetail dataDictDetail = new DataDictDetail();
                    dataDictDetail.setDictKey(custInfoBO.getId().toString());
                    dataDictDetail.setDictValue(custInfoBO.getName());
                    dataDictDetails.add(dataDictDetail);
                }
                dataDictDetailService.saveBatch(dataDictDetails);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
